//
//  MbRewardAd.m
//  MbAdsSdk
//
//  Created by 吴奶强 on 2021/12/17.
//

#import "MbNativeAd.h"
#import <BUAdSDK/BUAdSDK.h>
#import <KSAdSDK/KSAdSDK.h>
#import <ABUADSDK/ABUADSDK.h>
#import "GDTNativeExpressAd.h"
#import "GDTNativeExpressAdView.h"


@interface MbNativeAd()<BUNativeExpressAdViewDelegate,GDTNativeExpressAdDelegete,KSFeedAdsManagerDelegate,KSFeedAdDelegate,ABUNativeAdsManagerDelegate,ABUNativeAdViewDelegate>

@property(nonatomic,strong)BUNativeExpressAdManager *ttNativeAd;
@property(nonatomic,strong)KSFeedAdsManager *ksNativeAd;

@property (nonatomic, strong) GDTNativeExpressAd *gdtNativeAd;

@property(nonatomic,strong)ABUNativeAdsManager *gromoreNativeAd;

@property(nonatomic,assign)NSInteger index;

@property(nonatomic,strong)NSError *error;

@end

@implementation MbNativeAd


-(void)loadNative{
    if(!self.adItems || self.adItems.count == self.index  || !self.rootVC || !self.view){
        if(!self.error){
            self.error = [[NSError alloc]initWithDomain:@"配置信息错误" code:0 userInfo:nil];
        }
        if ([self.delegate respondsToSelector:@selector(nativeAdDidError:)]) {
            [self.delegate nativeAdDidError:self.error];
        }
        return;
    }
    AdItem *adItem = [self.adItems objectAtIndex:self.index];
    if(adItem.plat == Pangolin){
        BUAdSlot *slot1 = [[BUAdSlot alloc] init];
        slot1.ID = adItem.codeNo;
        slot1.AdType = BUAdSlotAdTypeFeed;
        BUSize *imgSize = [BUSize sizeBy:BUProposalSize_Feed228_150];
        slot1.imgSize = imgSize;
        slot1.position = BUAdSlotPositionFeed;
        if (!self.ttNativeAd) {
            self.ttNativeAd = [[BUNativeExpressAdManager alloc] initWithSlot:slot1 adSize:CGSizeMake(self.width, 0)];
        }
        self.ttNativeAd.adSize = CGSizeMake(self.width,0);
        self.ttNativeAd.delegate = self;
        [self.ttNativeAd loadAdDataWithCount:1];

    } else if(adItem.plat == GDT){
        // 支持视频广告的 PlacementId 会混出视频与图片广告
      self.gdtNativeAd = [[GDTNativeExpressAd alloc] initWithPlacementId:adItem.codeNo adSize:CGSizeMake(self.width, 0)];
      self.gdtNativeAd.delegate = self;
      // 配置视频播放属性
//      self.gdtNativeAd.maxVideoDuration = (NSInteger)self.maxVideoDurationSlider.value;  // 如果需要设置视频最大时长，可以通过这个参数来进行设置
//      self.gdtNativeAd.videoAutoPlayOnWWAN = self.videoAutoPlaySwitch.on;
//      self.gdtNativeAd.videoMuted = self.videoMutedSwitch.on;
      [self.gdtNativeAd loadAd:1];
       
        
    } else if(adItem.plat == KS){
        self.ksNativeAd = [[KSFeedAdsManager alloc] initWithPosId:adItem.codeNo
        size:CGSizeMake(self.width, 0)];
        self.ksNativeAd.delegate = self;
        [self.ksNativeAd loadAdDataWithCount:1];
    } else if(adItem.plat == GroMore){
        ABUAdUnit *slot1 = [[ABUAdUnit alloc] init];
        ABUSize *imgSize1 = [[ABUSize alloc] init];
        imgSize1.width = 1080;
        imgSize1.height = 1920;
        slot1.ID = adItem.codeNo;
        slot1.imgSize = imgSize1;
        slot1.getExpressAdIfCan = YES;
        slot1.adSize = CGSizeMake(self.width, 300);
        self.gromoreNativeAd = [[ABUNativeAdsManager alloc] initWithSlot:slot1];
        self.gromoreNativeAd.rootViewController = self.rootVC;
        self.gromoreNativeAd.startMutedIfCan = NO;
        self.gromoreNativeAd.delegate = self;
        //该逻辑用于判断配置是否拉取成功。如果拉取成功，可直接加载广告，否则需要调用setConfigSuccessCallback，传入block并在block中调用加载广告。SDK内部会在配置拉取成功后调用传入的block
        __weak typeof(self) weakself = self;
        if([ABUAdSDKManager configDidLoad]){
            [self.gromoreNativeAd loadAdDataWithCount:1];
        }else{
            [ABUAdSDKManager addConfigLoadSuccessObserver:self withAction:^(id  _Nonnull observer) {
               
                [weakself.gromoreNativeAd loadAdDataWithCount:1];
            }];
        }
    }
    self.index ++;
}


#pragma mark 穿山甲回调

- (void)nativeExpressAdSuccessToLoad:(NSObject *)nativeExpressAdManager views:(NSArray<__kindof NSObject *> *)views{
    if (views.count) {
        if([nativeExpressAdManager isKindOfClass:[BUNativeExpressAdManager class]]){
            
            [views enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

                BUNativeExpressAdView *expressView = (BUNativeExpressAdView *)obj;
                expressView.rootViewController = self.rootVC;
                // important: 此处会进行WKWebview的渲染，建议一次最多预加载三个广告，如果超过3个会很大概率导致WKWebview渲染失败。
                [expressView render];
            }];
        } else  if([nativeExpressAdManager isKindOfClass:[GDTNativeExpressAd class]]){
            [views enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
               GDTNativeExpressAdView *expressView = (GDTNativeExpressAdView *)obj;
               expressView.controller = self.rootVC;
               [expressView render];
                self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.width,
                                             expressView.frame.size.height);
                [self.view addSubview:expressView];
           }];
        }
       
    }
}

- (void)nativeExpressAdViewRenderSuccess:(UIView *)nativeExpressAdView{
    if(self.view){
        self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.width,
                                     nativeExpressAdView.frame.size.height);
        [self.view addSubview:nativeExpressAdView];
    }
    if ([self.delegate respondsToSelector:@selector(nativeAdDidLoad:)]) {
        [self.delegate nativeAdDidLoad:nativeExpressAdView];
    }
}

- (void)nativeExpressAdView:(BUNativeExpressAdView *)nativeExpressAdView dislikeWithReason:(NSArray<BUDislikeWords *> *)filterWords{
    if(self.view){
        [self.view removeFromSuperview];
        
    }
    if ([self.delegate respondsToSelector:@selector(nativeAdDislike:)]) {
        [self.delegate nativeAdDislike:nativeExpressAdView];
    }
}

- (void)nativeExpressAdFailToLoad:(BUNativeExpressAdManager *)nativeExpressAdManager error:(NSError *)error{
    self.error = error;
    [self loadNative];
}

#pragma mark 优量汇回调

- (void)nativeExpressAdViewClosed:(GDTNativeExpressAdView *)nativeExpressAdView{
    if(self.view){
        [self.view removeFromSuperview];
    }
    if ([self.delegate respondsToSelector:@selector(nativeAdDislike:)]) {
        [self.delegate nativeAdDislike:nativeExpressAdView];
    }
}


#pragma mark 快手回调
- (void)feedAdsManagerSuccessToLoad:(KSFeedAdsManager *)adsManager nativeAds:(NSArray<KSFeedAd *> *)feedAdDataArray{
    KSFeedAd *feedAd = feedAdDataArray[0];
    feedAd.delegate = self;
    
    if(self.view){
        
        self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.width,
                                     feedAd.feedView.frame.size.height);
        //NSLog(@"高度：%f", feedAd.feedView.frame.size.height);
        [self.view addSubview:feedAd.feedView];
    }
    if ([self.delegate respondsToSelector:@selector(nativeAdDidLoad:)]) {
        [self.delegate nativeAdDidLoad:feedAd.feedView];
    }
    
}

- (void)feedAdDislike:(KSFeedAd *)feedAd{
    if(self.view){
        [self.view removeFromSuperview];
    }
    if ([self.delegate respondsToSelector:@selector(nativeAdDislike:)]) {
        [self.delegate nativeAdDislike:feedAd.feedView];
    }
}

- (void)feedAdsManager:(KSFeedAdsManager *)adsManager didFailWithError:(NSError *)error{
    self.error = error;
    [self loadNative];
}


#pragma mark gromore回调
- (void)nativeAdsManagerSuccessToLoad:(ABUNativeAdsManager *)adsManager nativeAds:(NSArray<ABUNativeAdView *> *)nativeAdViewArray{
    ABUNativeAdView *expressView = nativeAdViewArray[0];
    expressView.delegate = self;
    // important: 此处会进行WKWebview的渲染，建议一次最多预加载三个广告，如果超过3个会很大概率导致WKWebview渲染失败。
    [expressView render];
}

- (void)nativeAdExpressViewRenderSuccess:(ABUNativeAdView *)nativeExpressAdView{
    if(self.view){
        self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.width,
                                     nativeExpressAdView.frame.size.height);
        [self.view addSubview:nativeExpressAdView];
    }
    if ([self.delegate respondsToSelector:@selector(nativeAdDidLoad:)]) {
        [self.delegate nativeAdDidLoad:nativeExpressAdView];
    }
}

- (void)nativeAdExpressViewDidClosed:(ABUNativeAdView *)nativeAdView closeReason:(NSArray<NSDictionary *> *)filterWords{
    if(self.view){
        [self.view removeFromSuperview];
    }
    if ([self.delegate respondsToSelector:@selector(nativeAdDislike:)]) {
        [self.delegate nativeAdDislike:nativeAdView];
    }
}

- (void)nativeAdsManager:(ABUNativeAdsManager *)adsManager didFailWithError:(NSError *)error{
    self.error = error;
    [self loadNative];
}


@end
